package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.*;
import cc.rengu.igas.bomp.common.dao.impl.*;
import cc.rengu.igas.bomp.common.entity.*;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.ImageInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntInspectInfoBean;
import cc.rengu.igas.bomp.facade.bean.MchntSignImageInfoBean;
import cc.rengu.igas.bomp.facade.request.MchntInspectInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntInspectInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/03/31
 */
public class MchntInspectInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntInspectInfoQueryRequest mchntInspectInfoQueryRequest = new MchntInspectInfoQueryRequest();
        ConvertUtil.convertOutput(mchntInspectInfoQueryRequest);
        return mchntInspectInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntInspectInfoQueryRequest mchntInspectInfoQueryRequest = (MchntInspectInfoQueryRequest) request;
        MchntInspectInfoQueryResponse mchntInspectInfoQueryResponse = new MchntInspectInfoQueryResponse();
        /*获取法人行号*/
        String instId = mchntInspectInfoQueryRequest.getHeader().getInstId();
        /*获取巡检任务号*/
        String inspectTaskNo = mchntInspectInfoQueryRequest.getInspectTaskNo();
        /*获取商户号*/
        String mchntNo = mchntInspectInfoQueryRequest.getMchntNo();
        /*获取用户信息*/
        String userId = mchntInspectInfoQueryRequest.getUserId();
        /*必填校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(inspectTaskNo)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*商户巡检任务详情信息查询*/
        InspectTaskDetailMapper inspectTaskDetailMapper = new InspectTaskDetailMapperImpl();
        InspectTaskDetail inspectTaskDetail = inspectTaskDetailMapper.selectInspectTaskDetailByPrimaryKey(instId, inspectTaskNo, mchntNo);
        if (null == inspectTaskDetail) {
            rglog.error("查询商户巡检任务详情信息失败");
            throw new BizException(RespCodeEnum.MCHNT_INSPECT_INFO_ERROR.getRespCode(), RespCodeEnum.MCHNT_INSPECT_INFO_ERROR.getRespDesc());
        }
        MchntInspectInfoBean mchntInspectInfo = new MchntInspectInfoBean();
        /*任务详情信息转换*/
        BeanUtil.beanCopy(inspectTaskDetail, mchntInspectInfo);
        /*根据任务号查询任务名称*/
        InspectTaskInfoMapper inspectTaskInfoMapper = new InspectTaskInfoMapperImpl();
        InspectTaskInfo inspectTaskInfo = inspectTaskInfoMapper.selectInspectTaskInfoByPrimaryKey(instId, inspectTaskNo);
        if (null != inspectTaskInfo) {
            mchntInspectInfo.setInspectTaskDesc(inspectTaskInfo.getInspectTaskDesc());
        }
        /*查询组织机构*/
        OrganizationMapper organizationMapper = new OrganizationMapperImpl();
        /*签约机构名称查询*/
        Organization signOrg = organizationMapper.selectOrganizationByPrimaryKey(instId, inspectTaskDetail.getSignInstId());
        if (null != signOrg) {
            mchntInspectInfo.setSignInstName(signOrg.getOrgName());
        }
        /*巡检机构名称查询*/
        Organization inspectOrg = organizationMapper.selectOrganizationByPrimaryKey(instId, inspectTaskDetail.getInspectInstId());
        if (null != inspectOrg) {
            mchntInspectInfo.setSignInstName(inspectOrg.getOrgName());
        }
        /*查询商户等级*/
        BompMchntLevelInfoMapper bompMchntLevelInfoMapper = new BompMchntLevelInfoMapperImpl();
        BompMchntLevelInfo bompMchntLevelInfo = bompMchntLevelInfoMapper.selectBompMchntLevelInfoByMchntLevelCode(instId, inspectTaskDetail.getMchntLevel());
        if (null != bompMchntLevelInfo) {
            mchntInspectInfo.setMchntLevelName(bompMchntLevelInfo.getMchntLevelName());
        }
        /*查询商户法人信息*/
        MchntSignInfoMapper mchntSignInfoMapper = new MchntSignInfoMapperImpl();
        MchntSignInfo mchntSignInfo = mchntSignInfoMapper.selectMchntSignInfoByPrimaryKey(instId, mchntNo);
        if (null != mchntSignInfo) {
            mchntInspectInfo.setArtifName(mchntSignInfo.getArtifName());
            mchntInspectInfo.setArtifCertNo(mchntSignInfo.getArtifCertNo());
        }

        /*商户签约影像信息查询*/
        List<MchntSignImageInfoBean> mchntSignImageInfoList = new ArrayList<>();
        MchntImageInfoMapper mchntImageInfoMapper = new MchntImageInfoMapperImpl();
        List<MchntImageInfo> mchntImageInfoList = mchntImageInfoMapper.selectMchntImageInfoByMchntNo(instId, mchntNo);
        if (!CollectionUtils.isEmpty(mchntImageInfoList)) {
            mchntImageInfoList.forEach(item -> convMchntSignImageInfo(item, mchntSignImageInfoList));
        }
        /*商户查询巡检影像信息*/
        List<MchntSignImageInfoBean> mchntInspectImageInfoList = new ArrayList<>();
        InspectRegisterInfoMapper inspectRegisterInfoMapper = new InspectRegisterInfoMapperImpl();
        List<InspectRegisterInfo> inspectRegisterInfoList = inspectRegisterInfoMapper.selectInspectRegisterInfoByMchntNo(instId, inspectTaskNo, mchntNo);
        if (!CollectionUtils.isEmpty(inspectRegisterInfoList)) {
            inspectRegisterInfoList.forEach(item -> convtInspectImageInfo(item, mchntInspectImageInfoList));
        }
        /*响应报文信息组装*/
        mchntInspectInfoQueryResponse.setMchntInspectImageInfoList(mchntInspectImageInfoList);
        mchntInspectInfoQueryResponse.setMchntSignImageInfoList(mchntSignImageInfoList);
        mchntInspectInfoQueryResponse.setMchntInspectInfo(mchntInspectInfo);
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<MchntInspectInfoQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntInspectInfoQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntInspectInfoQueryResponse mchntInspectInfoQueryResponse = (MchntInspectInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntInspectInfoQueryResponse);
    }

    /**
     * 商户签约影像信息转换
     *
     * @param mchntImageInfo         商户签约信息表数据
     * @param mchntSignImageInfoList 商户签约影像信息
     */
    private void convMchntSignImageInfo(MchntImageInfo mchntImageInfo, List<MchntSignImageInfoBean> mchntSignImageInfoList) {
        if (mchntSignImageInfoList.isEmpty()) {
            mchntSignImgAdd(mchntImageInfo, mchntSignImageInfoList);
        } else {
            Optional<MchntSignImageInfoBean> mchntSignImageInfoOpt = mchntSignImageInfoList.stream().filter(item -> item.getImageType().equals(mchntImageInfo.getImageType())).findFirst();
            if (mchntSignImageInfoOpt.isPresent()) {
                ImageInfoBean imageInfo = new ImageInfoBean();
                imageInfo.setImageIndex(String.valueOf(mchntImageInfo.getImageIndex()));
                imageInfo.setImageId(mchntImageInfo.getId());
                imageInfo.setImageDesc(mchntImageInfo.getImageDesc());
                imageInfo.setImagePath(mchntImageInfo.getImageRemotePath());
                mchntSignImageInfoOpt.get().getImageInfoList().add(imageInfo);
            } else {
                mchntSignImgAdd(mchntImageInfo, mchntSignImageInfoList);
            }
        }
    }

    /**
     * 新增商户影像信息
     *
     * @param mchntImageInfo         商户影像信息
     * @param mchntSignImageInfoList 签约影像信息列表
     */
    private void mchntSignImgAdd(MchntImageInfo mchntImageInfo, List<MchntSignImageInfoBean> mchntSignImageInfoList) {
        MchntSignImageInfoBean mchntSignImageInfo = new MchntSignImageInfoBean();
        mchntSignImageInfo.setImageType(mchntImageInfo.getImageType());
        List<ImageInfoBean> imageInfoList = new ArrayList<>();
        ImageInfoBean imageInfo = new ImageInfoBean();
        imageInfo.setImageIndex(String.valueOf(mchntImageInfo.getImageIndex()));
        imageInfo.setImageId(mchntImageInfo.getId());
        imageInfo.setImageDesc(mchntImageInfo.getImageDesc());
        imageInfo.setImagePath(mchntImageInfo.getImageRemotePath());
        imageInfoList.add(imageInfo);
        mchntSignImageInfo.setImageInfoList(imageInfoList);
        mchntSignImageInfoList.add(mchntSignImageInfo);
    }

    /**
     * 商户巡检影像信息转换
     *
     * @param inspectRegisterInfo       商户巡检信息表数据
     * @param mchntInspectImageInfoList 商户巡检影像信息
     */
    private void convtInspectImageInfo(InspectRegisterInfo inspectRegisterInfo, List<MchntSignImageInfoBean> mchntInspectImageInfoList) {
        if (!StringUtil.isEmptyOrNull(inspectRegisterInfo.getCollectType()) && "2".equals(inspectRegisterInfo.getCollectType())) {
            if (mchntInspectImageInfoList.isEmpty()) {
                mchntInspectImgAdd(inspectRegisterInfo, mchntInspectImageInfoList);
            } else {
                Optional<MchntSignImageInfoBean> mchntSignImageInfoOpt = mchntInspectImageInfoList.stream().filter(item -> item.getImageType().equals(inspectRegisterInfo.getCollectDesc())).findFirst();
                if (mchntSignImageInfoOpt.isPresent()) {
                    ImageInfoBean imageInfo = new ImageInfoBean();
                    imageInfo.setImageIndex(String.valueOf(mchntSignImageInfoOpt.get().getImageInfoList().size()));
                    imageInfo.setImageId(inspectRegisterInfo.getCollectId());
                    imageInfo.setImageDesc(inspectRegisterInfo.getCollectType());
                    imageInfo.setImagePath(inspectRegisterInfo.getCollectValue());
                    mchntSignImageInfoOpt.get().getImageInfoList().add(imageInfo);
                } else {
                    mchntInspectImgAdd(inspectRegisterInfo, mchntInspectImageInfoList);
                }
            }
        }
    }

    /**
     * 新增商户巡检影像信息
     *
     * @param inspectRegisterInfo       商户巡检影像信息
     * @param mchntInspectImageInfoList 商户巡检影像信息列表
     */
    private void mchntInspectImgAdd(InspectRegisterInfo inspectRegisterInfo, List<MchntSignImageInfoBean> mchntInspectImageInfoList) {
        MchntSignImageInfoBean mchntSignImageInfo = new MchntSignImageInfoBean();
        mchntSignImageInfo.setImageType(inspectRegisterInfo.getCollectDesc());
        List<ImageInfoBean> imageInfoList = new ArrayList<>();
        ImageInfoBean imageInfo = new ImageInfoBean();
        imageInfo.setImageIndex("0");
        imageInfo.setImageId(inspectRegisterInfo.getCollectId());
        imageInfo.setImageDesc(inspectRegisterInfo.getCollectType());
        imageInfo.setImagePath(inspectRegisterInfo.getCollectValue());
        imageInfoList.add(imageInfo);
        mchntSignImageInfo.setImageInfoList(imageInfoList);
        mchntInspectImageInfoList.add(mchntSignImageInfo);
    }
}
